{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 242. Valid Anagram 有效的字母异位词\n",
    "\n",
    "### 难度：Easy\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/valid-anagram\n",
    " - 英文：https://leetcode.com/problems/valid-anagram/\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定两个字符串 s 和 t ，编写一个函数来判断 t 是否是 s 的一个字母异位词。\n",
    "\n",
    "示例 1:\n",
    "输入: s = \"anagram\", t = \"nagaram\"\n",
    "输出: true\n",
    "\n",
    "示例 2:\n",
    "输入: s = \"rat\", t = \"car\"\n",
    "输出: false\n",
    "\n",
    "说明:\n",
    "你可以假设字符串只包含小写字母。\n",
    "\n",
    "进阶:\n",
    "如果输入字符串包含 unicode 字符怎么办？你能否调整你的解法来应对这种情况？\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    " - 一行瞬秒！\n",
    " - 适用 collections.Counter() 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return collections.Counter(s) == collections.Counter(t)\n",
    "    \n",
    "s = \"anagram\"\n",
    "t = \"nagaram\"\n",
    "ss = Solution()\n",
    "print(ss.isAnagram(s, t))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "> 思路 2\n",
    "\n",
    " - 同样是一行瞬秒！\n",
    " - 适用 sorted() 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return sorted(s) == sorted(t)\n",
    "s = \"anagram\"\n",
    "t = \"nagaram\"\n",
    "ss = Solution()\n",
    "print(ss.isAnagram(s, t))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 3\n",
    "\n",
    " - 用字数统计，因为只可能是 26 个字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def isAnagram(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        \n",
    "        charCnt = [0] * 26\n",
    "        \n",
    "        for i in range(len(s)):\n",
    "            charCnt[ord(s[i]) - 97] += 1\n",
    "            charCnt[ord(t[i]) - 97] -= 1 \n",
    "        \n",
    "        for cnt in charCnt:\n",
    "            if cnt != 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "s = \"anagram\"\n",
    "t = \"nagaram\"\n",
    "ss = Solution()\n",
    "print(ss.isAnagram(s, t))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
